package com.wyd.flowable;

import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.AbstractFlowableEventListener;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.engine.TaskService;
import org.flowable.task.service.impl.persistence.entity.TaskEntityImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class FlowableEventListener3 extends AbstractFlowableEventListener {
    @Autowired
    private TaskService taskService;

    @Override
    public void onEvent(FlowableEvent event) {
        log.info("进入全局监听 FlowableEventListener3 event={} class={} type={}", event, event.getClass(), event.getType().getClass());

        if (event.getType() == FlowableEngineEventType.TASK_CREATED) {
            // 用户任务创建完成
            if (event instanceof org.flowable.common.engine.impl.event.FlowableEntityEventImpl) {
                org.flowable.common.engine.impl.event.FlowableEntityEventImpl eventImpl = (org.flowable.common.engine.impl.event.FlowableEntityEventImpl) event;
                TaskEntityImpl taskEntity = (TaskEntityImpl) eventImpl.getEntity();

                log.info("taskEntity={}", taskEntity);

                // 测试发现 再次查询出执行人也是改变后的执行人
                String taskId = taskEntity.getId();
                String toDoUserId = taskService.createTaskQuery().taskId(taskId).singleResult().getAssignee();
                log.info("查询出的 originalAssignee={} assignee={}", taskEntity.getOriginalAssignee(), toDoUserId);
            }

            // 测试修改任务执行人看行不
            // 如果多次移交？

        }
    }

    @Override
    public boolean isFailOnException() {
        return true;
    }
}
